<template>
  <!-- 浇注计划填报表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
    :append-to-body="true"
  >
    <el-descriptions
      class="margin-top"
      title=""
      :column="2"
      size="large"
      v-show="props.isReadonly"
      border
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">炉号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.stoveNum }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">铁水重量</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.waterWeight }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">浇注区间时间</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.sectionTime }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">电炉残汤</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.electricStoveResidual }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">浇注模数</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.mouldQuantity }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">每一炉熔化时间分钟</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.meltingTime }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">浇注温度</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.temperature }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">实际总铁水量（出铁量+电炉残汤）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.actualTotalQuantity }}
        </div>
      </el-descriptions-item>
      <!--<el-descriptions-item>
        <template #label>
          <div class="cell-item">工时（每一炉熔化时间）</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.manHour }}
        </div>
      </el-descriptions-item>-->
    </el-descriptions>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="160"
      v-show="!props.isReadonly"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="炉号" prop="stoveNum">
            <el-input
              v-model="formData.stoveNum"
              placeholder="请输入炉号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="铁水重量" prop="waterWeight">
            <el-input
              v-model="formData.waterWeight"
              placeholder="请输入铁水重量"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="浇注区间时间" prop="sectionTime">
            <el-input
              v-model="formData.sectionTime"
              placeholder="请输入浇注区间时间"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电炉残汤" prop="electricStoveResidual">
            <el-input
              v-model="formData.electricStoveResidual"
              placeholder="请输入电炉残汤"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="浇注模数" prop="mouldQuantity">
            <el-input
              v-model="formData.mouldQuantity"
              placeholder="请输入浇注模数"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="每一炉熔化时间分钟" prop="meltingTime">
            <el-input
              v-model="formData.meltingTime"
              placeholder="请输入每一炉熔化时间分钟"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="浇注温度" prop="temperature">
            <el-input
              v-model="formData.temperature"
              placeholder="请输入浇注温度"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="实际总铁水量"
            prop="actualTotalQuantity"
          >
            <el-input
              v-model="formData.actualTotalQuantity"
              placeholder="请输入实际总铁水量（出铁量+电炉残汤）"
              clearable
            />
          </el-form-item>
        </el-col>
        <!--<el-col :span="12">
          <el-form-item label="工时" prop="manHour">
            <el-input
              v-model="formData.manHour"
              placeholder="请输入工时（每一炉熔化时间）"
              clearable
            />
          </el-form-item>
        </el-col>-->
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESPOURINGPLANREPORTADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  id: {
    type: String,
    default: () => {
      return "";
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  stoveNum: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
    { required: true, message: "请输入炉号", trigger: "blur" },
  ],
  waterWeight: [{ pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" }],
  electricStoveResidual: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  sectionTime: [{ pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" }],
  mouldQuantity: [{ pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" }],
  meltingTime: [{ pattern: /^\d{1,18}$/, message: "请输入整数" }],
  temperature: [{ pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" }],
  actualTotalQuantity: [
    { pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" },
  ],
  manHour: [{ pattern: /^\d{1,18}(\.\d{0,6})?$/, message: "请输入数字" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  planId: "",
  stoveNum: "",
  waterWeight: "",
  electricStoveResidual: "",
  sectionTime: "",
  mouldQuantity: "",
  meltingTime: "",
  temperature: "",
  actualTotalQuantity: "",
  manHour: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        planId,
        stoveNum,
        waterWeight,
        electricStoveResidual,
        sectionTime,
        mouldQuantity,
        meltingTime,
        temperature,
        actualTotalQuantity,
        manHour,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        planId,
        stoveNum,
        waterWeight,
        electricStoveResidual,
        sectionTime,
        mouldQuantity,
        meltingTime,
        temperature,
        actualTotalQuantity,
        manHour,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.planId = "";
  formData.stoveNum = "";
  formData.waterWeight = "";
  formData.electricStoveResidual = "";
  formData.sectionTime = "";
  formData.mouldQuantity = "";
  formData.meltingTime = "";
  formData.temperature = "";
  formData.actualTotalQuantity = "";
  formData.manHour = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 浇注计划填报保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      if (!formData.id) {
        formData.planId = props.id;
      }
      await request({
        url: "/mes/mespouringplanreport/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
